home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / hypercar / mactool / thinkcgu.sit / app ƒ / menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-17  |  3.6 KB  |  133 lines

  1. /*
  2. *    FILE:        menu.c
  3. *    AUTHOR:        R. Gonzalez
  4. *    CREATED:    August 25, 1990
  5. *
  6. *    Defines menu methods, required for all types of application.
  7. */
  8.  
  9. # include    <string.h>
  10. # include    <stdlib.h>
  11. # include    "menu.h"
  12. # include    "morestr.h"
  13.  
  14. /************************************************************************
  15. *    initialize menu
  16. ************************************************************************/
  17. boolean    Menu::init(void)
  18. {
  19.     int        i;
  20.     
  21.     num_menus = 0;
  22.     
  23.     for (i=1 ; i<=MAX_MENUS ; i++)
  24.         num_items[i] = 0;
  25.         
  26.     return TRUE;
  27. }
  28.  
  29. /************************************************************************
  30. *    add command to menu.  Space must be allocated statically by calling
  31. *    routine; e.g., argument may be a constant string like "Quit".
  32. *    By convention the menu numbers start at 1 while the item numbers
  33. *    start at 0.  The 0th item in each menu is the menu's heading.
  34. *    (This is ignored in command-line applications.)  It may be assoc-
  35. *    iated with the comfunc: NULL.  Be careful not to duplicate menu
  36. *    names (see the unimplemented function below), nor to exceed the
  37. *    MAX_MENUS and MAX_ITEMS limits, nor to leave any "empty" items
  38. *    whose item number is smaller than num_items[menu_num].
  39. ************************************************************************/
  40. boolean    Menu::log_command(int menu_num,int item,char *command,
  41.                             comfunc command_ptr)
  42. {
  43.     boolean        success;
  44.     
  45.     if (menu_num <= MAX_MENUS && item <= MAX_ITEMS)
  46.     {
  47.         if (menu_num > num_menus)
  48.             num_menus = menu_num;
  49.         if (item+1 > num_items[menu_num])
  50.             num_items[menu_num] = item + 1;
  51.         this->command[menu_num][item] = command;
  52.         this->command_ptr[menu_num][item] = command_ptr;
  53.         success = TRUE;
  54.     }
  55.     else
  56.         success = FALSE;
  57.     
  58.     return success;
  59. }
  60.  
  61. # ifdef never
  62. /************************************************************************
  63. *    check if command already exists - presently unused function
  64. ************************************************************************/
  65. boolean    Menu::command_exists(char *command)
  66. {
  67.     int        menu_num,
  68.             item;
  69.     boolean    exists = FALSE;
  70.             
  71.     for (menu_num=1 ; menu_num <= num_menus && !exists ; menu_num++)
  72.         for (item=0 ; item < num_items[menu_num] && !exists ; item++)
  73.             if (strsame(this->command[menu_num][item],command))
  74.                 exists = TRUE;
  75.     
  76.     return exists;
  77. }
  78. # endif
  79.  
  80. /************************************************************************
  81. *    get menu number
  82. ************************************************************************/
  83. int        Menu::get_menu(char *command)
  84. {
  85.     int        menu_num,
  86.             item;
  87.     boolean    done = FALSE;
  88.             
  89.     for (menu_num=1 ; menu_num <= num_menus && !done ; menu_num++)
  90.         for (item=1 ; item < num_items[menu_num] && !done ; item++)
  91.             if (strsame(this->command[menu_num][item],command))
  92.                 done = TRUE;
  93.     
  94.     if (done)
  95.         return menu_num-1;
  96.     else
  97.         return ILLEGAL;
  98. }
  99.  
  100. /************************************************************************
  101. *    get item number
  102. ************************************************************************/
  103. int        Menu::get_item(char *command)
  104. {
  105.     int        menu_num,
  106.             item;
  107.     boolean    done = FALSE;
  108.             
  109.     for (menu_num=1 ; menu_num <= num_menus && !done ; menu_num++)
  110.         for (item=1 ; item < num_items[menu_num] && !done ; item++)
  111.             if (strsame(this->command[menu_num][item],command))
  112.                 done = TRUE;
  113.     
  114.     if (done)
  115.         return item-1;
  116.     else
  117.         return ILLEGAL;
  118. }
  119.  
  120. /************************************************************************
  121. *    get pointer to command function associated with menu_num and item.
  122. ************************************************************************/
  123. comfunc        Menu::get_command_ptr(int menu_num,int item)
  124. {
  125.     if (menu_num < 1 || menu_num > num_menus)
  126.         return NULL;
  127.     else if (item < 1 || item >= num_items[menu_num])
  128.         return NULL;
  129.     else
  130.         return command_ptr[menu_num][item];
  131. }
  132.  
  133.